package chatserver.roles;


import chatserver.objects.User;
import java.util.ArrayList;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

public class ChannelRole extends Role {

    private Map<Rights.Channel, List<Integer>> rights;

    public ChannelRole(String name, String profile) {
        super(name, profile);

        rights = Collections.synchronizedMap(new EnumMap<Rights.Channel, List<Integer>>(Rights.Channel.class));
    }

    public ChannelRole(int sqlId, String name, String profile) {
        super(sqlId, name, profile);

        rights = Collections.synchronizedMap(new EnumMap<Rights.Channel, List<Integer>>(Rights.Channel.class));
    }

    public boolean addRight(Rights.Channel right) {

        return addRight(right, NO_RIGHT_MODIFIER);
    }

    public boolean addRight(Rights.Channel right, Integer modifier) {

        List<Integer> list;

        if (rights.containsKey(right)) {
            list = rights.get(right);
        } else {
            list = Collections.synchronizedList(new ArrayList<Integer>());
        }

        if (!list.contains(modifier)) {
            list.add(modifier);
            return true;
        }

        return false;
    }

    public boolean delRight(Rights.Channel right) {
        if (rights.containsKey(right)) {
            rights.remove(right);
        }
        return false;
    }

    public boolean delRight(Rights.Channel right, Integer modifier) {
        List<Integer> list;

        if (rights.containsKey(right)) {
            list = rights.get(right);
        } else {
            return false;
        }

        if (list.contains(modifier)) {
            list.remove(modifier);
            if (list.isEmpty()) {
                rights.remove(right);
            }
            return true;
        }

        return false;
    }
    
    public void setRights(Map<Rights.Channel, List<Integer>> rights)
    {
        Map<Rights.Channel, List<Integer>> tmpRights = Collections.synchronizedMap(new EnumMap<Rights.Channel, List<Integer>>(Rights.Channel.class));
        for(Entry<Rights.Channel, List<Integer>> entry : rights.entrySet())
        {
            tmpRights.put(entry.getKey(), Collections.synchronizedList(entry.getValue()));
        }
        this.rights = tmpRights;
    }
            

    @Override
    public final void loadRights() {
        Map<String, List<Integer>> sqlRights = mySql.loadRightsOfChannelRole(getSqlId());

        rights.clear();

        for (Map.Entry<String, List<Integer>> entry : sqlRights.entrySet()) {
            try {
                rights.put(Rights.Channel.valueOf(entry.getKey()), Collections.synchronizedList(entry.getValue()));
            } catch (Exception e) {
            }
        }
    }

    @Override
    public void saveRights() {
        if (getSqlId() == -1) {
            return;
        }

        Map<String, List<Integer>> sqlRights = new HashMap<String, List<Integer>>();

        for (Map.Entry<Rights.Channel, List<Integer>> entry : rights.entrySet()) {
            sqlRights.put(entry.getKey().toString(), entry.getValue());
        }

        mySql.saveRightsOfChannelRole(getSqlId(), sqlRights);
    }

    @Override
    public void save(boolean createIfNotExist) {
        if (getSqlId() == -1 && !createIfNotExist) {
            return;
        }
        
        if(getSqlId() == -1)
        {
          sqlId = mySql.saveChannelRole(getSqlId(), getName(), getProfile());
        }
        else
        {
            mySql.saveChannelRole(getSqlId(), getName(), getProfile());
        }

        saveRights();
    }

    public Map<Rights.Channel, List<Integer>> getRights() {
        return rights;
    }
    
    
    public boolean hasRight(Rights.Channel right)
    {     
        return hasRight(right, NO_RIGHT_MODIFIER);
    }
    
    
    public boolean hasRight(Rights.Channel right, int modifier)
    {       
       
        if(rights.containsKey(right))
        {
            List<Integer> modifiers = rights.get(right);
            if(modifiers.contains(modifier) || modifiers.contains(NO_RIGHT_MODIFIER))
            {
                return true;
            }
        }
        
        return false;
    }
     
    public boolean hasRightWithAnyModifier(Rights.Channel right)
    {
        
        if(rights.containsKey(right))
        {
                return true;
        }
        
        return false;
    }     
}
